import { defineConfig } from 'vite'
import legacyPlugin from '@vitejs/plugin-legacy' // 现代浏览器兼容
import { createVuePlugin } from 'vite-plugin-vue2' // 转换 vue 2.x 的语法
import ViteRequireContext from '@originjs/vite-plugin-require-context' // 转换 require.context 这种写法
import * as path from 'path'

const alias =
[

  { find: '@', replacement: path.resolve(__dirname, './src') },
  { find: /^~/, replacement: '' }
]

export default defineConfig({
  base: './', // index.html文件所在位置
  root: './', // js导入的资源路径，src
  resolve: {
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    alias
  },
  esbuild: {
    jsxFactory: 'h',
    jsxFragment: 'Fragment'
  },
  plugins: [
    ViteRequireContext({
      projectBasePath: process.cwd(),
      defaultRegExp: /^.*$/
    }),
    createVuePlugin({ jsx: true }),
    legacyPlugin({
      targets: ['Android > 39', 'Chrome >= 60', 'Safari >= 10.1', 'iOS >= 10.3', 'Firefox >= 54', 'Edge >= 15'],
      additionalLegacyPolyfills: ['regenerator-runtime/runtime']
    })
  ],
  build: {
    target: 'es2015',
    minify: 'terser',
    manifest: false,
    sourcemap: false,
    outDir: 'build',
    rollupOptions: {}
  },
  css: {
    preprocessorOptions: {
      less: {
        // 支持内联 JavaScript
        javascriptEnabled: true
      }
    }
  }
})
